package com.itfreer.gis.config;

import android.util.Log;

import com.itfreer.data.remote.DataService;
import com.itfreer.gis.GisDatabaseHelper;
import com.itfreer.gis.config.entity.BaseMapEntity;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.misc.TransactionManager;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * 定义离线底图服务
 */
public class BaseMapService {

    private static final String baseMapSN = "gisbasemap";
    private static final int pageSize = 100;

    public static final BaseMapService instance = new BaseMapService();
    private BaseMapService() {
    }

    /**
     * 获取所有信息
     * @return
     */
    public List<BaseMapEntity> getEntitys() {
        try {
            Dao<BaseMapEntity, String> dao = GisDatabaseHelper.getInstance().getDao(BaseMapEntity.class);
            List<BaseMapEntity> datas = dao.queryForAll();
            return datas;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 同步通知
     * @return
     */
    public Boolean syn() {
        //创建事务管理器
        TransactionManager transactionManager = new TransactionManager(GisDatabaseHelper.getInstance().getConnectionSource());
        //一个调用的事件
        Callable<Boolean> callable  = new Callable<Boolean>() {
            //如果异常被抛出 事件管理 就知道保存数据失败要回滚
            @Override
            public Boolean call() throws Exception {
                Dao<BaseMapEntity, String> dao = GisDatabaseHelper.getInstance().getDao(BaseMapEntity.class);
                // 清空表
                dao.queryRaw("delete from base_map");

                // 从远程获取表总数
                Integer rowCount = DataService.instance.getRowCounts(baseMapSN, null);
                Integer pageCount = (rowCount / pageSize) + (rowCount % pageSize > 0 ? 1 : 0);

                // 从远程获取表记录
                for(Integer pageIndex = 1;pageIndex<=pageCount;pageIndex++){
                    List<BaseMapEntity> datas = DataService.instance.getEntitys(baseMapSN,null,null,null, pageSize, pageIndex, BaseMapEntity.class);
                    if(datas!=null && datas.size()>0){
                        for(BaseMapEntity item : datas){
                            dao.create(item);
                        }
                    }
                }
                return true;
            }
        };

        Boolean result;
        try {
            //执行事件
            result =  transactionManager.callInTransaction(callable);
        } catch (SQLException e) {
            result = false;
            Log.w("YKF","事务保存异常");
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 更新状态
     * @param id
     */
    public BaseMapEntity getProjectById(String id){
        Dao<BaseMapEntity, String> dao = null;
        BaseMapEntity entity = null;
        try {
            dao = GisDatabaseHelper.getInstance().getDao(BaseMapEntity.class);
            entity = dao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  entity;
    }
}
